#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,urllib,re,md5,os
import MySQLdb
import ConfigParser
import time
class picTask():
    def __init__(self):
        self.url = "http://album.360quan.com/u/"
        self.imgRegular = "<a.*?href\=\"http://album.360quan.com/u/(.*?)\"[^>]*>"
        #self.imgRegular = r"<img.*?src\=\"http://i([0-3])\.(.*?)\"[^>]*>"
        self.page = r"<a.*?href\=\"(.*?)\"[^>]*>(.*?)</a>"
        self.pageNum = 20
        self.beginTime = time.time()
    
    def get(self):
        while True:
            limitSql = "select * from user_task where status=0 limit 0,20"
            db = self.dbConn()
            cursor = db.cursor()
            cursor.execute(limitSql)
            info = cursor.fetchall()
            db.close()
            if len(info) < 1:
                break
            self.mark(info)
            for row in info:
                pageUrl = row[1]
                keyes = row[2]
                domain = self.url+pageUrl
                pageNum = self.isHrml(domain)
                try:
                    for p in range(1,pageNum+1):
                        pg = p
                        beginDomain = domain+"/page/"+str(pg)
                        htmlBegin = self.getHtml(beginDomain)
                        matchList = self.matchCode(self.imgRegular,self.getPicList(htmlBegin))
                        self.insert(matchList)
                        print "当前",time.time() - self.beginTime
                    self.upStatus(pageUrl)
                    print row[0],pageUrl
                except:
                    print "not page"
    
    def isHrml(self,domain):
        eventNum = 3
        for e in range(0,eventNum):
            html = self.getHtml(domain)
            pageNum = int(self.getPageNum2(html))
            if pageNum > 0:
                return pageNum
            if e > eventNum:
                return 0
            print "重试%d %s" % (e,domain)
            time.sleep(1)
    
    def mark(self,lists):
        for row in lists:
            pageUrl = row[1]
            keyes = row[2]
            sql = "update user_task set status=1 where u_space='"+pageUrl+"'"
            db = self.dbConn()
            cursor = db.cursor()
            cursor.execute(sql)
            db.close()
    
    def upStatus(self,k):
        sql = "update user_task set status=-1 where u_space='"+k+"'"
        db = self.dbConn()
        cursor = db.cursor()
        cursor.execute(sql)
        db.close()
    
    def getPicList(self,html):
        begin = html.split("<div class=\"albumList\">")
        end   = begin[1].split("<div class=\"leaf\">")
        return end[0]
    
    def getPageNum2(self,html):
        try:
            matchPageList = html.split("(<font id=\"pic_count\">")[1].split("</font>张)")[0]
        except:
            print "not photo"
        matchPageNum = 0
        try:
            if int(matchPageList) > 20:
                matchPageNum = int(matchPageList) / int(self.pageNum) + 1
            else:
                matchPageNum = 1
        except:
            matchPageNum = 1
        return matchPageNum
        
    
    def getPageNum(self,html):
        splitHtml = html.split("<div class=\"leaf\">")
        endHtml = splitHtml[1].split("<a class=\"pagtion\"")
        pageMatch = self.matchCode(self.page,endHtml[0])
        #pageMatch.sort()
        lenNum = len(pageMatch)
        return pageMatch[lenNum-1]
    
    def insert(self,code):
        for i in code:
            sql = "insert into img_list (url) values ('"+i+"')"
            db = self.dbConn()
            cursor = db.cursor()
            cursor.execute(sql)
            db.close()
    
    #get file code
    def getHtml(self,domain):
        try:
            wp = urllib.urlopen(domain)
        except:
            print 'url exception'
        content = wp.read()
        wp.close()
        return content
    
    #get config file
    def initConfig(self,configFile):
        config = ConfigParser.ConfigParser();
        config.readfp(open(configFile));
        return config;
    
    def matchCode(self,regular,content):
        str = re.compile(regular,re.I|re.S)
        return str.findall(content)
    
    def dbConn(self,dbServer="default"):
        config = self.initConfig("../config/_db.ini")
        user = str(config.get(dbServer,"user"))
        db   = str(config.get(dbServer,"db"))
        passwd   = str(config.get(dbServer,"passwd"))
        host   = str(config.get(dbServer,"host"))
        conn = MySQLdb.connect(user=user, db=db, passwd=passwd, host=host)
        return conn
    
t = picTask()
t.get()